home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / System.java < prev    next >
Text File  |  1998-09-22  |  24KB  |  597 lines

  1. /*
  2.  * @(#)System.java    1.73 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. import java.io.*;
  18. import java.util.Properties;
  19. import java.util.StringTokenizer;
  20.  
  21. /**
  22.  * The <code>System</code> class contains several useful class fields 
  23.  * and methods. It cannot be instantiated. 
  24.  * <p>
  25.  * Among the facilities provided by the <code>System</code> class 
  26.  * are standard input, standard output, and error output streams; 
  27.  * access to externally defined "properties"; a means of 
  28.  * loading files and libraries; and a utility method for quickly 
  29.  * copying a portion of an array. 
  30.  *
  31.  * @author  Arthur van Hoff 
  32.  * @version 1.73, 07/01/98
  33.  * @since   JDK1.0
  34.  */
  35. public final
  36. class System {
  37.     /** Don't let anyone instantiate this class */
  38.     private System() {
  39.     }
  40.  
  41.     /**
  42.      * The "standard" input stream. This stream is already 
  43.      * open and ready to supply input data. Typically this stream 
  44.      * corresponds to keyboard input or another input source specified by 
  45.      * the host environment or user. 
  46.      *
  47.      * @since   JDK1.0
  48.      */
  49.     public final static InputStream in = nullInputStream();
  50.  
  51.     /**
  52.      * The "standard" output stream. This stream is already 
  53.      * open and ready to accept output data. Typically this stream 
  54.      * corresponds to display output or another output destination 
  55.      * specified by the host environment or user. 
  56.      * <p>
  57.      * For simple stand-alone Java applications, a typical way to write 
  58.      * a line of output data is: 
  59.      * <ul><code>System.out.println(data)</code></ul>
  60.      * <p>
  61.      * See the <code>println</code> methods in class <code>PrintStream</code>. 
  62.      *
  63.      * @see     java.io.PrintStream#println()
  64.      * @see     java.io.PrintStream#println(boolean)
  65.      * @see     java.io.PrintStream#println(char)
  66.      * @see     java.io.PrintStream#println(char[])
  67.      * @see     java.io.PrintStream#println(double)
  68.      * @see     java.io.PrintStream#println(float)
  69.      * @see     java.io.PrintStream#println(int)
  70.      * @see     java.io.PrintStream#println(long)
  71.      * @see     java.io.PrintStream#println(java.lang.Object)
  72.      * @see     java.io.PrintStream#println(java.lang.String)
  73.      * @since   JDK1.0
  74.      */
  75.     public final static PrintStream out = nullPrintStream();
  76.  
  77.     /**
  78.      * The "standard" error output stream. This stream is already 
  79.      * open and ready to accept output data. 
  80.      * <p>
  81.      * Typically this stream corresponds to display output or another 
  82.      * output destination specified by the host environment or user. By 
  83.      * convention, this output stream is used to display error messages 
  84.      * or other information that should come to the immediate attention 
  85.      * of a user even if the principal output stream, the value of the 
  86.      * variable <code>out</code>, has been redirected to a file or other 
  87.      * destination that is typically not continuously monitored. 
  88.      *
  89.      * @since   JDK1.0
  90.      */
  91.     public final static PrintStream err = nullPrintStream();
  92.  
  93.     /* The security manager for the system.
  94.      */
  95.     private static SecurityManager security = null;
  96.  
  97.     /**
  98.      * Reassigns the "standard" input stream.
  99.      *
  100.      * @since   JDK1.1
  101.      */
  102.     public static void setIn(InputStream in) {
  103.     checkIO();
  104.     setIn0(in);
  105.     }
  106.  
  107.     /**
  108.      * Reassigns the "standard" output stream.
  109.      *
  110.      * @since   JDK1.1
  111.      */
  112.     public static void setOut(PrintStream out) {
  113.     checkIO();
  114.     setOut0(out);
  115.     }
  116.  
  117.     /**
  118.      * Reassigns the "standard" error output stream.
  119.      *
  120.      * @since   JDK1.1
  121.      */
  122.     public static void setErr(PrintStream err) {
  123.     checkIO();
  124.     setErr0(err);
  125.     }
  126.  
  127.     private static void checkIO() {
  128.     if (security != null) {
  129.         /* REMIND: this should have its own security check call */
  130.         security.checkExec("setio");
  131.     }
  132.     }
  133.  
  134.     private static native void setIn0(InputStream in);
  135.     private static native void setOut0(PrintStream out);
  136.     private static native void setErr0(PrintStream err);
  137.  
  138.     /**
  139.      * Sets the System security.
  140.      * If a security manager has already been established for the 
  141.      * currently running Java application, a <code>SecurityException</code> 
  142.      * is thrown. Otherwise, the argument is established as the current 
  143.      * security manager. If the argument is <code>null</code> and no 
  144.      * security manager has been established, then no action is taken and 
  145.      * the method simply returns. 
  146.      *
  147.      * @param      s   the security manager.
  148.      * @exception  SecurityException  if the security manager has already
  149.      *               been set.
  150.      * @since   JDK1.0
  151.      */
  152.     public static void setSecurityManager(SecurityManager s) {
  153.     if (security != null) {
  154.         throw new SecurityException("SecurityManager already set");
  155.     }
  156.     security = s;
  157.     }
  158.  
  159.     /**
  160.      * Gets the system security interface.
  161.      *
  162.      * @return  if a security manager has already been established for the
  163.      *          current application, then that security manager is returned;
  164.      *          otherwise, <code>null</code> is returned.
  165.      * @since   JDK1.0
  166.      */
  167.     public static SecurityManager getSecurityManager() {
  168.     return security;
  169.     }
  170.  
  171.     /**
  172.      * Returns the current time in milliseconds.
  173.      * <p>
  174.      * See the description of the class <code>Date</code> for a discussion 
  175.      * of slight discrepancies that may arise between "computer 
  176.      * time" and coordinated universal time (UTC). 
  177.      *
  178.      * @return  the difference, measured in milliseconds, between the current
  179.      *          time and midnight, January 1, 1970 UTC.
  180.      * @see     java.util.Date
  181.      * @since   JDK1.0
  182.      */
  183.     public static native long currentTimeMillis();
  184.  
  185.     /** 
  186.      * Copies an array from the specified source array, beginning at the
  187.      * specified position, to the specified position of the destination array.
  188.      * A subsequence of array components are copied from the source 
  189.      * array referenced by <code>src</code> to the destination array 
  190.      * referenced by <code>dst</code>. The number of components copied is 
  191.      * equal to the <code>length</code> argument. The components at 
  192.      * positions <code>srcOffset</code> through 
  193.      * <code>srcOffset+length-1</code> in the source array are copied into 
  194.      * positions <code>dstOffset</code> through 
  195.      * <code>dstOffset+length-1</code>, respectively, of the destination 
  196.      * array. 
  197.      * <p>
  198.      * If the <code>src</code> and <code>dst</code> arguments refer to the 
  199.      * same array object, then the copying is performed as if the 
  200.      * components at positions <code>srcOffset</code> through 
  201.      * <code>srcOffset+length-1</code> were first copied to a temporary 
  202.      * array with <code>length</code> components and then the contents of 
  203.      * the temporary array were copied into positions 
  204.      * <code>dstOffset</code> through <code>dstOffset+length-1</code> of the 
  205.      * argument array. 
  206.      * <p>
  207.      * If any of the following is true, an 
  208.      * <code>ArrayStoreException</code> is thrown and the destination is 
  209.      * not modified: 
  210.      * <ul>
  211.      * <li>The <code>src</code> argument refers to an object that is not an 
  212.      *     array. 
  213.      * <li>The <code>dst</code> argument refers to an object that is not an 
  214.      *     array. 
  215.      * <li>The <code>src</code> argument and <code>dst</code> argument refer to 
  216.      *     arrays whose component types are different primitive types. 
  217.      * <li>The <code>src</code> argument refers to an array with a primitive 
  218.      *     component type and the <code>dst</code> argument refers to an array 
  219.      *     with a reference component type. 
  220.      * <li>The <code>src</code> argument refers to an array with a reference 
  221.      *     component type and the <code>dst</code> argument refers to an array 
  222.      *     with a primitive component type. 
  223.      * </ul>
  224.      * <p>
  225.      * Otherwise, if any of the following is true, an 
  226.      * <code>ArrayIndexOutOfBoundsException</code> is 
  227.      * thrown and the destination is not modified: 
  228.      * <ul>
  229.      * <li>The <code>srcOffset</code> argument is negative. 
  230.      * <li>The <code>dstOffset</code> argument is negative. 
  231.      * <li>The <code>length</code> argument is negative. 
  232.      * <li><code>srcOffset+length</code> is greater than 
  233.      *     <code>src.length</code>, the length of the source array. 
  234.      * <li><code>dstOffset+length</code> is greater than 
  235.      *     <code>dst.length</code>, the length of the destination array. 
  236.      * </ul>
  237.      * <p>
  238.      * Otherwise, if any actual component of the source array from 
  239.      * position <code>srcOffset</code> through 
  240.      * <code>srcOffset+length-1</code> cannot be converted to the component 
  241.      * type of the destination array by assignment conversion, an 
  242.      * <code>ArrayStoreException</code> is thrown. In this case, let 
  243.      * <b><i>k</i></b> be the smallest nonnegative integer less than 
  244.      * length such that <code>src[srcOffset+</code><i>k</i><code>]</code> 
  245.      * cannot be converted to the component type of the destination 
  246.      * array; when the exception is thrown, source array components from 
  247.      * positions <code>srcOffset</code> through
  248.      * <code>srcOffset+</code><i>k</i><code>-1</code> 
  249.      * will already have been copied to destination array positions 
  250.      * <code>dstOffset</code> through
  251.      * <code>dstOffset+</code><i>k</I><code>-1</code> and no other 
  252.      * positions of the destination array will have been modified. 
  253.      *
  254.      * @param      src:      the source array.
  255.      * @param      srcpos    start position in the source array.
  256.      * @param      dest      the destination array.
  257.      * @param      destpos   start position in the destination data.
  258.      * @param      length    the number of array elements to be copied.
  259.      * @exception  ArrayIndexOutOfBoundsException  if copying would cause
  260.      *               access of data outside array bounds.
  261.      * @exception  ArrayStoreException  if an element in the <code>src</code>
  262.      *               array could not be stored into the <code>dest</code> array
  263.      *               because of a type mismatch.
  264.      * @since      JDK1.0
  265.      */
  266.     public static native void arraycopy(Object src, int src_position,
  267.                                         Object dst, int dst_position,
  268.                                         int length);
  269.  
  270.     /**
  271.      * Returns the same hashcode for the given object as
  272.      * would be returned by the default method hashCode(),
  273.      * whether or not the given object's class overrides
  274.      * hashCode().
  275.      * The hashcode for the null reference is zero.
  276.      *
  277.      * @since   JDK1.1
  278.      */
  279.     public static native int identityHashCode(Object x);
  280.  
  281.     /**
  282.      * System properties. The following properties are guaranteed to be defined:
  283.      * <dl>
  284.      * <dt>java.version        <dd>Java version number
  285.      * <dt>java.vendor        <dd>Java vendor specific string
  286.      * <dt>java.vendor.url    <dd>Java vendor URL
  287.      * <dt>java.home        <dd>Java installation directory
  288.      * <dt>java.class.version    <dd>Java class version number
  289.      * <dt>java.class.path    <dd>Java classpath
  290.      * <dt>os.name        <dd>Operating System Name
  291.      * <dt>os.arch        <dd>Operating System Architecture
  292.      * <dt>os.version        <dd>Operating System Version
  293.      * <dt>file.separator    <dd>File separator ("/" on Unix)
  294.      * <dt>path.separator    <dd>Path separator (":" on Unix)
  295.      * <dt>line.separator    <dd>Line separator ("\n" on Unix)
  296.      * <dt>user.name        <dd>User account name
  297.      * <dt>user.home        <dd>User home directory
  298.      * <dt>user.dir        <dd>User's current working directory
  299.      * </dl>
  300.      */
  301.  
  302.     private static Properties props;
  303.     private static native Properties initProperties(Properties props);
  304.  
  305.     /**
  306.      * Determines the current system properties. 
  307.      * <p>
  308.      * If there is a security manager, its 
  309.      * <code>checkPropertiesAccess</code> method is called with no 
  310.      * arguments. This may result in a security exception. 
  311.      * <p>
  312.      * The current set of system properties is returned as a 
  313.      * <code>Properties</code> object. If there is no current set of 
  314.      * system properties, a set of system properties is first created and 
  315.      * initialized. 
  316.      * <p>
  317.      * This set of system properties always includes values for the 
  318.      * following keys: 
  319.      * <table>
  320.      * <tr><th>Key</th>
  321.      *     <th>Description of Associated Value</th></tr>
  322.      * <tr><td><code>java.version</code></td>
  323.      *     <td>Java version number</td></tr>
  324.      * <tr><td><code>java.vendor</code></td>
  325.      *     <td>Java vendor-specific string</td></tr>
  326.      * <tr><td><code>java.vendor.url</code></td>
  327.      *     <td>Java vendor URL</td></tr>
  328.      * <tr><td><code>java.home</code></td>
  329.      *     <td>Java installation directory</td></tr>
  330.      * <tr><td><code>java.class.version</code></td>
  331.      *     <td>Java class format version number</td></tr>
  332.      * <tr><td><code>java.class.path</code></td>
  333.      *     <td>Java class path</td></tr>
  334.      * <tr><td><code>os.name</code></td>
  335.      *     <td>Operating system name</td></tr>
  336.      * <tr><td><code>os.arch</code></td>
  337.      *     <td>Operating system architecture</td></tr>
  338.      * <tr><td><code>os.version</code></td>
  339.      *     <td>Operating system version</td></tr>
  340.      * <tr><td><code>file.separator</code></td>
  341.      *     <td>File separator ("/" on UNIX)</td></tr>
  342.      * <tr><td><code>path.separator</code></td>
  343.      *     <td>Path separator (":" on UNIX)</td></tr>
  344.      * <tr><td><code>line.separator</code></td>
  345.      *     <td>Line separator ("\n" on UNIX)</td></tr>
  346.      * <tr><td><code>user.name</code></td>
  347.      *     <td>User's account name</td></tr>
  348.      * <tr><td><code>user.home</code></td>
  349.      *     <td>User's home directory</td></tr>
  350.      * <tr><td><code>user.dir</code></td>
  351.      *     <td>User's current working directory</td></tr>
  352.      * </table>
  353.      *
  354.      * @exception  SecurityException  if the current thread cannot access the
  355.      *               system properties.
  356.      * @see        java.lang.SecurityException
  357.      * @see        java.lang.SecurityManager#checkPropertiesAccess()
  358.      * @see        java.util.Properties
  359.      * @since      JDK1.0
  360.      */
  361.     public static Properties getProperties() {
  362.     if (security != null) {
  363.         security.checkPropertiesAccess();
  364.     }
  365.     return props;
  366.     }
  367.  
  368.     /**
  369.      * Sets the system properties to the <code>Properties</code> 
  370.      * argument. 
  371.      * <p>
  372.      * First, if there is a security manager, its 
  373.      * <code>checkPropertiesAccess</code> method is called with no 
  374.      * arguments. This may result in a security exception. 
  375.      * <p>
  376.      * The argument becomes the current set of system properties for use 
  377.      * by the <code>getProperty</code> method. If the argument is 
  378.      * <code>null</code>, then the current set of system properties is 
  379.      * forgotten. 
  380.      *
  381.      * @param      props   the new system properties.
  382.      * @exception  SecurityException  if the current thread cannot set the
  383.      *               system properties.
  384.      * @see        java.lang.SecurityException
  385.      * @see        java.lang.SecurityManager#checkPropertiesAccess()
  386.      * @since      JDK1.0
  387.      */
  388.     public static void setProperties(Properties props) {
  389.     if (security != null) {
  390.         security.checkPropertiesAccess();
  391.     }
  392.     System.props = props;
  393.     }
  394.     
  395.     /**
  396.      * Gets the system property indicated by the specified key. 
  397.      * <p>
  398.      * First, if there is a security manager, its 
  399.      * <code>checkPropertyAccess</code> method is called with the key as 
  400.      * its argument. This may result in a system exception. 
  401.      * <p>
  402.      * If there is no current set of system properties, a set of system 
  403.      * properties is first created and initialized in the same manner as 
  404.      * for the <code>getProperties</code> method. 
  405.      *
  406.      * @param      key   the name of the system property.
  407.      * @return     the string value of the system property,
  408.      *             or <code>null</code> if there is no property with that key.
  409.      * @exception  SecurityException  if the current thread cannot access the
  410.      *               system properties or the specified property.
  411.      * @see        java.lang.SecurityException
  412.      * @see        java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
  413.      * @see        java.lang.System#getProperties()
  414.      * @since      JDK1.0
  415.      */
  416.     public static String getProperty(String key) {
  417.     if (security != null) {
  418.         security.checkPropertyAccess(key);
  419.     }
  420.     return props.getProperty(key);
  421.     }
  422.     
  423.     /**
  424.      * Gets the system property indicated by the specified key. 
  425.      * <p>
  426.      * First, if there is a security manager, its 
  427.      * <code>checkPropertyAccess</code> method is called with the 
  428.      * <code>key</code> as its argument. 
  429.      * <p>
  430.      * If there is no current set of system properties, a set of system 
  431.      * properties is first created and initialized in the same manner as 
  432.      * for the <code>getProperties</code> method. 
  433.      *
  434.      * @param      key   the name of the system property.
  435.      * @param      def   a default value.
  436.      * @return     the string value of the system property,
  437.      *             or the default value if there is no property with that key.
  438.      * @exception  SecurityException  if the current thread cannot access the
  439.      *               system properties or the specified property.
  440.      * @see        java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
  441.      * @see        java.lang.System#getProperties()
  442.      * @since      JDK1.0
  443.      */
  444.     public static String getProperty(String key, String def) {
  445.     if (security != null) {
  446.         security.checkPropertyAccess(key); 
  447.     }
  448.     return props.getProperty(key, def);
  449.     }
  450.     
  451.     /**
  452.      * Gets an environment variable. An environment variable is a
  453.      * system dependent external variable that has a string value.
  454.      * 
  455.      * @param   the name of the environment variable.
  456.      * @return     the value of the variable, or null if the variable is
  457.      *        not defined.
  458.      * @since   JDK1.0
  459.      * @deprecated
  460.      */
  461.     public static String getenv(String name) {
  462.     throw new Error("getenv no longer supported, use properties and -D instead: " + name);
  463.     }
  464.  
  465.     /**
  466.      * Terminates the currently running Java Virtual Machine. The 
  467.      * argument serves as a status code; by convention, a nonzero status 
  468.      * code indicates abnormal termination. 
  469.      * <p>
  470.      * This method calls the <code>exit</code> method in class 
  471.      * <code>Runtime</code>. This method never returns normally. 
  472.      *
  473.      * @param      status   exit status.
  474.      * @exception  SecurityException  if the current thread cannot exit with
  475.      *               the specified status.
  476.      * @see        java.lang.Runtime#exit(int)
  477.      * @since      JDK1.0
  478.      */
  479.     public static void exit(int status) {
  480.     Runtime.getRuntime().exit(status);
  481.     }
  482.  
  483.     /**
  484.      * Runs the garbage collector.
  485.      * <p>
  486.      * Calling the <code>gc</code> method suggests that the Java Virtual 
  487.      * Machine expend effort toward recycling unused objects in order to 
  488.      * make the memory they currently occupy available for quick reuse. 
  489.      * When control returns from the method call, the Java Virtual 
  490.      * Machine has made a best effort to reclaim space from all unused 
  491.      * objects.
  492.      *
  493.      * @see     java.lang.Runtime#gc()
  494.      * @since   JDK1.0
  495.      */
  496.     public static void gc() {
  497.     Runtime.getRuntime().gc();
  498.     }
  499.  
  500.     /**
  501.      * Runs the finalization methods of any objects pending finalization.
  502.      * <p>
  503.      * Calling this method suggests that the Java Virtual Machine expend 
  504.      * effort toward running the <code>finalize</code> methods of objects 
  505.      * that have been found to be discarded but whose <code>finalize</code> 
  506.      * methods have not yet been run. When control returns from the 
  507.      * method call, the Java Virtual Machine has made a best effort to 
  508.      * complete all outstanding finalizations. 
  509.      *
  510.      * @see     java.lang.Runtime#runFinalization()
  511.      * @since   JDK1.0
  512.      */
  513.     public static void runFinalization() {
  514.     Runtime.getRuntime().runFinalization();
  515.     }
  516.  
  517.     /**
  518.      * Enable or disable finalization on exit; doing so specifies that the
  519.      * finalizers of all objects that have finalizers that have not yet been
  520.      * automatically invoked are to be run before the Java runtime exits.
  521.      * By default, finalization on exit is disabled.
  522.      * @see     java.lang.Runtime#exit(int)
  523.      * @see     java.lang.Runtime#gc()
  524.      * @since   JDK1.1
  525.      */
  526.     public static void runFinalizersOnExit(boolean value) {
  527.     Runtime.getRuntime().runFinalizersOnExit(value);
  528.     }
  529.  
  530.     /**
  531.      * Loads the specified filename as a dynamic library. The filename 
  532.      * argument must be a complete pathname. 
  533.      * <p>
  534.      * This method calls the <code>load</code> method in class 
  535.      * <code>Runtime. </code> 
  536.      *
  537.      * @param      filename   the file to load.
  538.      * @exception  SecurityException  if the current thread cannot load the
  539.      *               specified dynamic library.
  540.      * @exception  UnsatisfiedLinkError  if the file does not exist.
  541.      * @see        java.lang.Runtime#load(java.lang.String)
  542.      * @since      JDK1.0
  543.      */
  544.     public static void load(String filename) {
  545.     Runtime.getRuntime().load(filename);
  546.     }
  547.  
  548.     /**
  549.      * Loads the system library specified by the <code>libname</code> 
  550.      * argument. The manner in which a library name is mapped to the 
  551.      * actual system library is system dependent. 
  552.      *
  553.      * @param      libname   the name of the library.
  554.      * @exception  SecurityException  if the current thread cannot load the
  555.      *               specified dynamic library.
  556.      * @exception  UnsatisfiedLinkError  if the library does not exist.
  557.      * @see        java.lang.Runtime#loadLibrary(java.lang.String)
  558.      * @since      JDK1.0
  559.      */
  560.     public static void loadLibrary(String libname) {
  561.     Runtime.getRuntime().loadLibrary(libname);
  562.     }
  563.  
  564.     /**
  565.      * The following two methods exist because in, out, and err must be
  566.      * initialized to null.  The compiler, however, cannot be permitted to
  567.      * inline access to them, since they are later set to more sensible values
  568.      * by initializeSystemClass().
  569.      */
  570.     private static InputStream nullInputStream() throws NullPointerException {
  571.     if (currentTimeMillis() > 0)
  572.         return null;
  573.     throw new NullPointerException();
  574.     }
  575.  
  576.     private static PrintStream nullPrintStream() throws NullPointerException {
  577.     if (currentTimeMillis() > 0)
  578.         return null;
  579.     throw new NullPointerException();
  580.     }
  581.  
  582.     /**
  583.      * Initialize the system class.  Called after thread initialization.
  584.      */
  585.     private static void initializeSystemClass() {
  586.     props = new Properties();
  587.     initProperties(props);
  588.     FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
  589.     FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
  590.     FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
  591.     setIn0(new BufferedInputStream(fdIn));
  592.     setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
  593.     setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));
  594.     }
  595.  
  596. }
  597.